package org.ielse.frame.view;

import org.ielse.frame.R;
import org.ielse.frame.util.BaseUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.view.View.MeasureSpec;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class TitleLayout extends LinearLayout {

	private DisplayMetrics outMetrics = new DisplayMetrics();

	private FrameLayout layCenter, layLeft, layRight;

	public TitleLayout(Context context) {
		this(context, null);
	}

	public TitleLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(outMetrics);

		setBackgroundColor(0xffe26400);
		LayoutInflater.from(context).inflate(R.layout.layout_title, this);
		layLeft = (FrameLayout) findViewById(R.id.lay_left);
		layCenter = (FrameLayout) findViewById(R.id.lay_center);
		layRight = (FrameLayout) findViewById(R.id.lay_right);
	}

	@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		final int heightSize = BaseUtil.isScreenVertical((Activity) getContext()) ? (int) (outMetrics.widthPixels * 0.125f)
				: (int) (outMetrics.heightPixels * 0.125f);
		final int widthSize = MeasureSpec.getSize(widthMeasureSpec);

		setMeasuredDimension(widthSize, heightSize);
	}

	public void setTextTitle(int textId) {
		TextView txTitle = new TextView(getContext());
		txTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		txTitle.setText(textId);
		txTitle.setTextColor(Color.WHITE);
		layCenter.removeAllViews();
		layCenter.addView(txTitle);
	}

}
